/**
 * <pre>
 * @(#)MiniChart.java 1.01 2009-12-1
 * 
 * Copyright (c) 1998, 2009 Regular Individual. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Contributor(s):
 *     Haixu Huang
 * </pre>
 **/
package cn.sh.huang.chart;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;

import javax.swing.JComponent;

import cn.sh.huang.chart.ext.MouseEventListener;
import cn.sh.huang.chart.view.AbstractView;
import cn.sh.huang.chart.view.container.Container;

/**
 * <pre>
 * MiniChart ...
 * 
 * @version 1.01
 * </pre>
 */
public class MiniChart extends JComponent implements Container
{
    private AbstractView view = null;
    private Rectangle rect = new Rectangle();

    public MiniChart()
    {
        this.setDoubleBuffered(true);
    }

    public Dimension getPreferredSize()
    {
        return (view != null) ? view.getMinimumSize() : null;
    }

    public void paint(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g;
        Dimension size = this.getSize();
        Rectangle rect = this.getVisibleRect();
        g2d.setColor(Color.WHITE);
        g2d.fill(rect);
        if (view != null) {
            if (!this.rect.equals(rect)) {
                this.rect = rect;
                view.setViewSize(size.width, size.height);
            }
            // Rendering graphics by view obj.
            view.render((Graphics2D) g);
        }
    }

    public void update(Graphics g)
    {
        paint(g);
    }

    public void setView(AbstractView view)
    {
        if (this.view != null) {
            this.view.dispose();
        }
        view.setViewContainer(this);
        this.view = view;
        this.repaint();
    }

    public void addMouseEventListener(MouseEventListener l)
    {
        this.addMouseListener(l);
        this.addMouseMotionListener(l);
        this.addMouseWheelListener(l);
    }

    public void removeMouseEventListener(MouseEventListener l)
    {
        removeMouseListener(l);
        removeMouseMotionListener(l);
        removeMouseWheelListener(l);
    }

    public Point getGridAnchor()
    {
        return getViewAnchor(); // gridAnchor of MiniChart is just the same of viewAnchor
    }

    public Rectangle getViewRect(AbstractView view)
    {
        return getVisibleRect();
    }

    /*
     * (non-Javadoc)
     * @see cn.sh.huang.chart.view.container.Container#getGridRect(cn.sh.huang.chart.view.AbstractView)
     */
    public Rectangle getGridRect(AbstractView view)
    {
        Dimension size = getSize();
        return new Rectangle(0, 0, size.width, size.height);
    }

    /*
     * (non-Javadoc)
     * @see cn.sh.huang.chart.view.container.Container#getViewAnchor()
     */
    public Point getViewAnchor()
    {
        return new Point(0, 0);
    }

    /*
     * (non-Javadoc)
     * @see cn.sh.huang.chart.view.container.Container#getViewShift()
     */
    public Point getViewShift()
    {
        return new Point(0, 0);
    }
}
